package com.example.project_out_in;

import java.util.ArrayList;
import java.util.List;

import Business.bSQL_Heper;
import Entities.EUsers_;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.building_msmart.ulti.Alertdialog_;
import com.building_msmart.ulti.MD5;

public class changePass extends Activity{
	List<EUsers_> listUser = new ArrayList<EUsers_>();
	 bSQL_Heper sqlheper;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set View to register.xml
        setContentView(R.layout.changepass);
        sqlheper = new bSQL_Heper(this);
        Bundle extras = getIntent().getExtras();
    
        if (extras != null)
        {
        	EUsers_ v = (EUsers_)extras.getSerializable("listUser");
        	listUser.add(v);
        	
        }
        
        TextView loginScreen = (TextView) findViewById(R.id.link_to_login);
        Button btnchangepass = (Button) findViewById(R.id.btnChagepass);
        final EditText txtPass1 = (EditText)findViewById(R.id.change_mk1);
        final EditText txtPass2 = (EditText)findViewById(R.id.change_mk2);
        final EditText txtPass3 = (EditText)findViewById(R.id.change_mk3);
        // Listening to Login Screen link
        loginScreen.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				// Switching to Login Screen/closing register screen
				finish();
			}
		});
        
        btnchangepass.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Alertdialog_ alt = new Alertdialog_();
				if(!txtPass1.getText().toString().equals("") && !txtPass2.getText().toString().equals("") && !txtPass3.getText().toString().equals("")){
					if(txtPass2.getText().toString().equals(txtPass3.getText().toString()) && txtPass2.getText().toString().length()>5)
					{
					String uname="";
					String oldpass="";
					int idd=0;
					for (EUsers_ cn : listUser) {
						uname=cn.getUserName();
						oldpass=cn.getPassword();
						idd = cn.getID();
		        	}
					final MD5 m = new MD5();
					String pass = m.md5(txtPass2.getText().toString());
					String pass1 = m.md5(txtPass1.getText().toString());
					if(oldpass.equals(pass1))
					{
						//Log.d("pass: ", m.md5(txtPass1.getText().toString()) +" pass cu:"+oldpass);
						
						int b=sqlheper.changepass(uname,pass,idd);
						//Log.d("ket qua: ", b+"");
						if(b==1)
						finish();
						else
							alt.alert(R.drawable.tick, "Thông báo", "Đã xảy ra lỗi, vui lòng thử lại", changePass.this);
					}
					else
						alt.alert(R.drawable.tick, "Thông báo", "Mật khẩu cũ không đúng", changePass.this);
					}
					else
						alt.alert(R.drawable.tick, "Thông báo", "Mật khẩu mới không đúng và mật khảu phải lớn hơn 5 ký tự", changePass.this);
				}
				else
					alt.alert(R.drawable.tick, "Thông báo", "Vui lòng điền đầy đủ thông tin", changePass.this);
			
				
			}
		});
    }
}
